//
//  ZYNViewControllerForHotSongListDetail.m
//  VideoPic
//
//  Created by dllo on 16/3/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNViewControllerForHotSongListDetail.h"
#import "ZYNCollectionViewCellForHotListMore.h"
#import "NetworkingHandle.h"
#import "ZYNModelForMoreHotSongList.h"
#import "ZYNViewControllerForPlayBase.h"
#import "ZYNViewControllerForAllSongList.h"
#import "ZYNCollectionReusableViewForHeader.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ZYNViewControllerForHotSongListDetail ()<UICollectionViewDelegate, UICollectionViewDataSource, ZYNCollectionReusableViewForHeaderDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *arrForData;
@property (nonatomic, strong) NSString *strForType;
@property (nonatomic, strong) NSString *strForUrl;
@property (nonatomic, strong) UICollectionReusableView *header;
@end

@implementation ZYNViewControllerForHotSongListDetail
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.type == YES) {
        self.strForType = self.strForChangjing;
    } else {
        
        self.strForType = @"最热";
    }
    [self handleData];
    [self createCollection];
}
- (void)handleData {
    self.arrForData = [NSMutableArray array];
    self.strForUrl = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?s=s200&size=10&hid=5218286763020790&utdid=UwlxIo9OkikBAIGWDJh6Ypjh&q=tag:%@&net=2&app=ttpod&v=v8.4.0.2016022715&alf=alf700145&tid=0&uid=358851054311604&f=f508&page=1&imsi=460078024203819", self.strForType];
    NSLog(@"%@", self.strForUrl);
    [NetworkingHandle handleWorkData:self.strForUrl AndBody:nil value:nil handleBlock:^(id result) {
        for (NSDictionary *dic in [result objectForKey:@"data"]) {
            ZYNModelForMoreHotSongList *model = [[ZYNModelForMoreHotSongList alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrForData addObject:model];
            
        }
        
        [self.collectionView reloadData];
        
    }];
  
}
- (void)createFlowLayout {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, WIDTH / 2 + 20);
    
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 10;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.flowLayout.headerReferenceSize = CGSizeMake(50, 50);
    
    
}
- (void)createCollection {
    [self createFlowLayout];
    self.collectionView  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[ZYNCollectionViewCellForHotListMore class] forCellWithReuseIdentifier:@"pool1"];
    [self.collectionView registerClass:[ZYNCollectionReusableViewForHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader"];
   
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForData.count;
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    ZYNCollectionReusableViewForHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
    header.delegate = self;
    return header;

}
- (void)handleAll {
    
    ZYNViewControllerForAllSongList *allSong = [[ZYNViewControllerForAllSongList alloc] init];
    [self.navigationController pushViewController:allSong animated:YES];
}
- (void)handleHotOrNew:(UIButton *)button {
    
    if (button.tag == 100) {
        
        self.strForType = @"最热";
        [self handleData];
    } else {
        
        self.strForType = @"最新";
        [self handleData];
        
    }
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZYNCollectionViewCellForHotListMore *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    ZYNModelForMoreHotSongList *model = [self.arrForData objectAtIndex:indexPath.row];
    [cell passModel:model];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZYNViewControllerForPlayBase *detail = [[ZYNViewControllerForPlayBase alloc] init];
    ZYNModelForMoreHotSongList *model = [self.arrForData objectAtIndex:indexPath.row];
    detail.selected = NO;
    detail.strForSongId = model._id;
    [self.navigationController pushViewController:detail animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
